package project.Dao;/*
 *  @Author Mr.Wu
 *  @QQ 1507468405
 *  @Creat 2021/10/26 - 11:34
 */

import Utils.JDBCTemp;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import project.Domain.GroupClass;
import project.Domain.ID_Name;
import project.Domain.User;

import java.util.List;

public class groupDao {
    //    创建JdbcTemplate对象用于执行sql语句
    JdbcTemplate template = new JdbcTemplate(JDBCTemp.getDataSource());

    //    增加  项目
    public boolean add(GroupClass project) {
        String sql = "insert into xm05_project_info(leaderID,name,property,startTime,dueTime,workload,progress) VALUES(?,?,?,?,?,?,?)";
        System.out.println("\nleaderID:"+project.getLeaderID()+"\ngetName:"+project.getName()+
                "\ngetProperty:"+project.getProperty()+"\ngetDueTime:"+project.getDueTime()+
                "\ngetWorkload:"+project.getWorkload()+ "\ngetStartTime:"+project.getStartTime());
        int update = template.update(sql, project.getLeaderID(), project.getName(), project.getProperty(), project.getStartTime(), project.getDueTime(),
                project.getWorkload(), 0);
//        System.out.println(update);
        return update > 0;
    }
    //    增加资金管理
    public boolean addProperty(int id, String project_name, String leader_name, int total) {
        System.out.println("正在尝试添加资产。。。");
        String sql = "insert into xm05_property_info(id,pro_name,pro_manager,asset_tot,asset_used,rate) VALUES(?,?,?,?,?,?)";
        int update = template.update(sql, id, project_name, leader_name, total,"0","0");
        System.out.println(update);
        return update > 0;
    }
    public List<ID_Name> getID_Name(){
        System.out.println("---------开始查询数据-------");
        String sql = "select b.id as id,a.name as projectName,b.name as leaderName from xm05_project_info a,xm05_user_file b where a.leaderID = b.id";
//        将查询的结果封装为一个userList集合返回
        List<ID_Name> list = template.query(sql, new BeanPropertyRowMapper<ID_Name>(ID_Name.class));
        System.out.println("-------共找到"+list.size()+"项数据------");
        return list;
    }

    public List<ID_Name> getGroup(int leaderID){
        System.out.println("---------开始查询数据-------");
        String sql = "select b.id as id,a.name as projectName,b.name as leaderName from xm05_project_info a,xm05_user_file b where a.leaderID = b.id and a.leaderID = ?";
//        将查询的结果封装为一个userList集合返回
        List<ID_Name> list = template.query(sql, new BeanPropertyRowMapper<ID_Name>(ID_Name.class), leaderID);
        System.out.println("-------共找到"+list.size()+"项数据------");
        return list;
    }
    //    删除项目
    public boolean delete(int id) {
        String sql = "delete from xm05_project_info where leaderID = ?";
        int update = template.update(sql, id);
//        根据所影响的行数来判断是否删除成功
        return update > 0;
    }
    //    删除资金管理
    public boolean deleteProperty(int id) {
        String sql = "delete from xm05_property_info where id=?";
        int update = template.update(sql, id);
//        根据所影响的行数来判断是否删除成功
        return update > 0;
    }
    //    更新项目进度
    public boolean updateProgress(int p,int id) {
        String sql = "update xm05_project_info set progress=? where leaderID = ?";
        int update = template.update(sql, p, id);
//        根据所影响的行数来判断是否删除成功
        return update > 0;
    }
    /////////////获取全部项目组的全部信息
    public List<GroupClass> getGroupList(){
        String sql = "select a.*,b.`name` as leaderName from xm05_project_info a,xm05_user_file b where a.leaderID = b.id";
//        将查询的结果封装为一个projectList集合返回
        List<GroupClass> list = template.query(sql, new BeanPropertyRowMapper<GroupClass>(GroupClass.class));
        return list;
    }
    /////////////获取组长id索引的项目组的全部信息
    public List<GroupClass> getIndexGroup(int id){
        String sql = "select a.*,b.`name` as leaderName from xm05_project_info a,xm05_user_file b where a.leaderID = b.id and b.id = ?";
//        将查询的结果封装为一个projectList集合返回
        List<GroupClass> list = template.query(sql, new BeanPropertyRowMapper<GroupClass>(GroupClass.class), id);
        return list;
    }
}
